package org.jverify.utility;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Convenience class to load and return a file as a <code>Properties</code>
 * object.
 * 
 * @author Yiannis Mavroukakis
 */
public final class Settings {

	private static final int IO_ERROR = -10;
	private static final int NO_PROPERTIES = -1;
	private static final Logger LOG = LoggerFactory.getLogger(Settings.class);

	private Settings() {
	}

	/**
	 * Static method that handles the loading of a file as a
	 * <code>Properties</code> object. If an error occurs, the application is
	 * terminated.
	 * 
	 * @param fileName
	 *            <code>String</code> containing the full filepath of the file.
	 * @return <code>Properties</code> object containing the application
	 *         configuration.
	 */
	public static Properties load(final String fileName) {

		final Properties prop = new Properties();
		FileInputStream fsIn = null;
		try {
			fsIn = new FileInputStream(fileName);
			prop.load(fsIn);
		}
		catch (final FileNotFoundException e) {
			LOG.error("Could not find settings file");
			System.exit(NO_PROPERTIES); // NOPMD by imavroukakis on 10/18/10
										// 11:21 AM
		}
		catch (final IOException e) {
			LOG.error("An IO error occured,Exiting.");
			System.exit(IO_ERROR); // NOPMD by imavroukakis on 10/18/10 11:21 AM
		}
		finally {
			try {
				fsIn.close();
			}
			catch (final IOException e) {
				LOG.error("Could not close filestream");
			}
		}
		return prop;
	}

}
